package com.ny20231079080.dao.Impl;

import com.ny20231079080.dao.ManagerDao;
import com.ny20231079080.entity.Managers;
import homework.six.DbUntil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ManagerDaoImpl implements ManagerDao {
    private DbUntil  dbUntil = new DbUntil();
    @Override
    public Managers login(String name, String password) throws Exception {
        Connection  con = dbUntil.getCon();
        String sql = "select * from managers where name = ? and password = ?";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setString(1, name);
        preparedStatement.setString(2, password);
        ResultSet resultSet = preparedStatement.executeQuery();
        Managers  manager = new Managers();
        if (resultSet.next()){
            int id = resultSet.getInt("manager_id");
            String mname = resultSet.getString("name");
            String phone = resultSet.getString("phone");
            String mpassword = resultSet.getString("password");
            manager.setManager_id(id);
            manager.setName(mname);
            manager.setPhone(phone);
            manager.setPassword(mpassword);
            return manager;
        }
        return null;

    }

    @Override
    public List<Managers> findAllManagers() throws SQLException, ClassNotFoundException {
        Connection con = dbUntil.getCon();
        String sql = "select * from managers";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        List<Managers> managersList = new ArrayList<Managers>();
        while (resultSet.next()) {
            Managers manager = new Managers();
            int id = resultSet.getInt("manager_id");
            manager.setManager_id(id);
            manager.setName(resultSet.getString("name"));
            manager.setPhone(resultSet.getString("phone"));
            manager.setPassword(resultSet.getString("password"));
            managersList.add(manager);
        }
        return managersList;
    }

    @Override
    public int insertManager(Managers manager) throws SQLException, ClassNotFoundException {
        Connection con = dbUntil.getCon();
        String sql = "insert into managers(name,phone,password) values(?,?,?)";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setString(1, manager.getName());
        preparedStatement.setString(2, manager.getPhone());
        preparedStatement.setString(3, manager.getPassword());
        int rows = preparedStatement.executeUpdate();
        return rows;
    }



    @Override
    public int deleteManager(int manager_id) throws SQLException, ClassNotFoundException {
        Connection  con = dbUntil.getCon();
        String sql = "delete from managers where manager_id = ?";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setInt(1, manager_id);
        return preparedStatement.executeUpdate();

    }

    @Override
    public int updateManager(Managers manager) throws SQLException, ClassNotFoundException {
        Connection  con = dbUntil.getCon();
        String sql = "update managers set name = ?,phone = ?,password = ? where manager_id = ?";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setString(1, manager.getName());
        preparedStatement.setString(2, manager.getPhone());
        preparedStatement.setString(3, manager.getPassword());
        preparedStatement.setInt(4, manager.getManager_id());
        return preparedStatement.executeUpdate();
    }
}
